package menus;

import com.sun.lwuit.Form;
import java.io.IOException;
import util.GoogleMaps;
import util.Menu;
import util.locationService;

/**
 *
 * @author nialldeasy
 */
public class Map extends Menu {

    double lat = 0, lon = 0;
    int zoom = 15;
    locationService myLoc = null;
    boolean started = false;
    boolean refresh = false;
    boolean gps_on = false;
    GoogleMaps map = null;
    public boolean success =false;

    protected void execute(Form f) {
        myLoc = new locationService(false);
        map = new GoogleMaps();
        refresh = true;
        update();
    }

    public void recycleBin() {
        myLoc = null;
        map = null;
    }

    public Map() {
        gps_on = true;
    }

    public Map(double lattitude, double longitude) {

        lat = lattitude;
        lon = longitude;
        gps_on = false;
    }

    public Map(String landmark) {
        double[] d = locate(landmark);
        if (d != null) {
            lat = d[0];
            lon = d[1];
            success=true;
             map = new GoogleMaps();
            System.out.println(landmark+" ["+lat+" , "+lon+"]" +" mapped");
        }
        gps_on = false;
    }

    public double[] locate(String landmark) {
        return new Landmarks().get_position(landmark);
    }

 
    //...............................................................................//

    public void updatePosition(Form f) {
        double LAT = myLoc.latitude; double LON = myLoc.longitude;

        if (gps_on) {
            if(LAT!=lat||LON!=lon){
                refresh=true;
            }
        }
        

    }
    public void draw(){
            try {
                //System.out.println("Updating Map: Lat: " + lat + " Lon: " + lon+ " zoom: "+zoom);
                f.setBgImage(map.retrieveStaticImage(200, 320, lat, lon, zoom, "png32"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            refresh = false;

    }
    public void update() {
        // if(f!=null){
        if (!started) {
            started = true;
            myLoc.start();
        }

         draw();
        // updatePosition(f);
         f.repaint();
        //  }
    }

    public void Key_CENTRE(Form f) {
        lat++;
        lon = 10;
        refresh = true;
        gps_on = true;
        update();
    }

    public void Key_UP(Form f) {
        lat++;
        refresh = true;
        update();
    }

    public void Key_DOWN(Form f) {
        lat--;
        refresh = true;
        update();
    }

    public void Key_LEFT(Form f) {
        lon--;
        refresh = true;
        update();
    }

    public void Key_RIGHT(Form f) {
        lon++;
        refresh = true;
        update();
    }

    public void Key_1(Form f) {
        System.out.println("one pressed");
        zoom++;
        refresh = true;
        update();
    }

    public void Key_4(Form f) {
        super.Key_4(f);
    }

    public void Key_3(Form f) {
        super.Key_3(f);
    }

    public void Key_0(Form f) {
        super.Key_0(f);
        System.out.println(0);
    }

    public void Key_2(Form f) {
        zoom--;
        refresh = true;
        update();
    }
}
